package com.example.es.controller;


import com.example.es.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Autowired
    private IndexService indexService;


    @ResponseBody
    @GetMapping("/prepareData")
    public String prepareData() {
        String result = indexService.prepareData();
        return result;
    }


    @GetMapping("/search")
    public String search(@RequestParam("keywords") String keywords, @RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize, Model model) {
        List<Map<String, Object>> list = indexService.searchByKeywords(keywords, PageRequest.of(pageNum, pageSize));
        model.addAttribute("datas", list);
        return "search";
    }
}
